import http from "@/http/index.ts"
import {ConveyorPricing, ConveyorPricingListParam, ExportRequest, ExportFieldInfo} from '@/api/conveyorPricing/type.ts'

// 新增输送件核价
export const addApi = (data: ConveyorPricing) => {
    return http.post("/conveyorPricing/createConveyorPricing", data)
}

// 分页条件查询列表
export const getListApi = (data: ConveyorPricingListParam) => {
    return http.get("/conveyorPricing/getConveyorPricingList", data)
}

// 编辑输送件核价
export const editApi = (data: ConveyorPricing) => {
    return http.put("/conveyorPricing/updateConveyorPricing", data)
}

// 删除输送件核价
export const deleteApi = (id: number) => {
    return http.delete(`/conveyorPricing/deleteConveyorPricing/${id}`)
}

// 新增输送件核价
export const getDetial = (data: ConveyorPricing) => {
    return http.post("/conveyorPricing/getDetial", data)
}

// 导入输送件核价
export const importConveyorPricing = (file: File) => {
    const formData = new FormData()
    formData.append('file', file)
    return http.upload("/conveyorPricing/import", formData)
}

// 导出输送件核价（返回blob）
export const exportConveyorPricing = () => {
    const url = `${http['instance'].defaults.baseURL}/conveyorPricing/export`
    window.location.href = url
    return Promise.resolve()
}

// 自定义字段导出输送件核价
export const exportCustomConveyorPricing = (data: ExportRequest) => {
    // 使用POST请求，需要使用Blob响应类型
    return http.post("/conveyorPricing/exportCustom", data, { responseType: 'blob' }).then(response => {
        // 创建Blob链接并触发下载
        const blob = new Blob([response.data], { type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' })
        const link = document.createElement('a')
        link.href = window.URL.createObjectURL(blob)
        link.download = '输送件核价数据.xlsx'
        link.click()
        window.URL.revokeObjectURL(link.href)
        return Promise.resolve()
    })
}

// 获取可导出的字段列表
export const getExportFields = () => {
    return http.get("/conveyorPricing/getExportFields")
}

// 下载导入模板（返回blob）
export const downloadTemplate = () => {
    const url = `${http['instance'].defaults.baseURL}/conveyorPricing/downloadTemplate`
    window.location.href = url
    return Promise.resolve()
}

// 更新输送件核价状态
export const updateStatusApi = (params: { id: number; activate: number }) => {
    return http.put('/conveyorPricing/updateStatus', params)
}